<?php
class quizActions extends BaseActions
{
    public  $m;
    public function __construct()
    {
        parent::__construct();
        Files::factory('Quiz');
		$this->m = new Quiz();
		foreach ($_SESSION as $k=>$v){
			if (substr($k, 0,2)=='pc') unset($_SESSION[$k]);
		}
    }
    /**
    *
    * 测试首页
    */
    public function actionsIndex()
    {
        
    }

    /**
    *
    * 测试
    */
    public function actionsAdd()
    {
    	if (Http::get('select')){
    		
    		$this->addJs(MVC_WWW_HOST.'js/jquery/jquery.form.js');
	    	$this->addCss(MVC_WWW_HOST.'js/simplemodal/css/basic.css');	
	    	
	    	$this->addJs(MVC_WWW_HOST.'js/simplemodal/jquery.simplemodal.js');
	        $cates = $this->m->getAllCate();
	        $this->assign('cates', $cates);
	        $this->display('add'.Http::get('select').'.tpl.php');
    	}
    }
    public function actionsSave(){
    	$select = Http::get('select');    	
    	//print_r($_POST);
    	if ($select){
    		$method = 'save_'.$select;
    		$this->$method();
    	}
    	die();
    }
    public function save_1(){
    	$cate = Http::post('cate');
    	$sub1 = Http::post('sub1');
    	$sub2 = Http::post('sub2');
    	$sub3 = Http::post('sub3');
    	if(isEmpty($sub1)){
    		die('主标题不能空') ;
    	}
    	//print_r(Http::prepost('pc_score')) ;
    	if(!Http::prepost('p_title') or !Http::prepost('p_score') or !Http::prepost('pc_score') ){
    		die('请设置题目或题目的分数') ;
    	}
    	//大题
    	$p_sort= Http::prepost('p_sort');
    	$p_title = Http::prepost('p_title');
    	$p_score = Http::prepost('p_score');
    	//小题
    	$pc_sort= Http::prepost('pc_sort');
    	$pc_title = Http::prepost('pc_title');
    	$pc_score = Http::prepost('pc_score');
    	
    	asort($p_sort);
    	$tmpQuiz = array();
    	foreach ($p_sort as $k=>$v){    		
    		$tmpId = str_replace('p_sort_','',$k);
    		//echo $tmpId."\r\n";
    		$tmpTitle = Http::post('p_title_'.$tmpId);
    		$tmpScore = Http::post('p_score_'.$tmpId);
    		$tmpQues = $this->getPCQues($tmpId,1);
    		if (!$tmpQues) {
    			die('请设置小题');
    		}
    		$tmpQuiz[]=array('title'=>$tmpTitle,'score'=>$tmpScore,'question'=>$tmpQues);
    	} 
    	
    	if ($this->m->getQuizByTitle($sub1,user('uid'))){
    		die('请不要重复添加！');
    	}
    	
    	$quiz_id = $this->m->db->insert('q_quiz', array('sub1'=>$sub1,'sub2'=>$sub2,'sub3'=>$sub3,'score'=>0,'cate'=>$cate,'uid'=> user('uid'),'timeline'=>date("Y-m-d H:i:s")),true);
    	$totalScore=0;
    	foreach ($tmpQuiz as $k=>$v){
    		$big_id = $this->m->db->insert('q_quiz_big', array('quiz_id'=>$quiz_id,'score'=>$v['score'],'title'=>$v['title']),true);
    		foreach ($v['question'] as $kk=>$vv){
    			$this->m->db->insert('q_quiz_small', array('ques_id'=>$vv['id'],'score'=>$vv['score'],'quiz_id'=>$quiz_id, 'big_id'=>$big_id),true);
    			$totalScore = $totalScore+$vv['score'];
    		}
    	}
    	$this->m->db->updateData('q_quiz', array('score'=>$totalScore), " id = $quiz_id ");
    	echo '保存成功！';	
    }
    
    public function getPCQues($pid,$isCheckScore=0){
    	$pc_sort = Http::prepost('pc_sort_'.$pid.'_');
    	$tmpQues = array();
    	
    	if ($pc_sort){
    		asort($pc_sort);
	    	foreach ($pc_sort as $k=>$v){
	    		$tmpId = str_replace('pc_sort_'.$pid.'_','',$k);
	    		$tmpScore =  Http::post('pc_score_'.$pid.'_'.$tmpId);
	    		if ($tmpScore=='' and $isCheckScore) die('请设置小题分数');
	    		$tmpQues[] = array('id'=>$tmpId,'score'=>$tmpScore);
	    	}
    	}    	
    	return $tmpQues;
    	
    }
    
    public function actionsAddcate(){
    	$arr = array('rs'=>0,'html'=>'添加失败');
    	if (Http::get()){    		
    		$name = Http::get('savecate');
    		$uid = user('uid');
    		if ($name){
    			$data = array('name'=>$name,'uid'=>$uid,'islast'=>1);  
        		if ($this->m->getCateByUserAndName($uid,$name)){
        			$arr = array('rs'=>0,'html'=>'添加失败，重复添加');
        		}else {
        			$this->m->db->updateData('q_quiz_cate', array('islast'=>0), " WHERE uid = '$uid'  ");
    	    		$id = $this->m->db->insert('q_quiz_cate', $data,true);	    		
    	    		if ($id){
    	    			$arr = array('rs'=>1,'html'=>'添加成功','id'=>$id);
    	    		}
    	    	}  			
    		} else{
    		    $arr = array('rs'=>0,'html'=>'分类名称不能为空');
    		}
    		
    	}    	
    	echo json_encode($arr);
    	die();
    }
}#classEnd